package cn.icanci.loopstack.ddk.admin.biz.service.impl;

import cn.icanci.loopstack.ddk.admin.biz.mapping.config.LogMapping;
import cn.icanci.loopstack.ddk.admin.biz.service.LogService;
import cn.icanci.loopstack.ddk.admin.biz.utils.EnvUtils;
import cn.icanci.loopstack.ddk.admin.dal.mapper.entity.LogDO;
import cn.icanci.loopstack.ddk.admin.dal.mapper.mapper.LogMapper;
import cn.icanci.loopstack.ddk.common.model.PageList;
import cn.icanci.loopstack.ddk.common.model.log.LogOperateVO;
import cn.icanci.loopstack.ddk.common.utils.PaginatorUtils;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

/**
 * @author icanci
 * @since 1.0 Created in 2023/01/07 23:32
 */
@Service
public class LogServiceImpl implements LogService {
    @Resource
    private LogMapper  logMapper;

    @Resource
    private LogMapping logMapping;

    @Override
    public void log(LogOperateVO logOperate) {
        logMapper.insert(logMapping.vo2do(logOperate));
    }

    @Override
    public PageList<LogOperateVO> queryPage(String module, String targetId, int pageNum, int pageSize) {
        LogDO operate = new LogDO();
        operate.setModule(module);
        operate.setTargetId(targetId);
        operate.setEnv(EnvUtils.getEnv());

        pageNum = Math.max(pageNum - 1, 0);

        List<LogDO> ret = logMapper.selectPage(operate, pageNum, pageSize);

        return PaginatorUtils.formatPaginator(logMapping.dos2vos(ret), pageNum, pageSize, logMapper.total(operate));
    }
}
